/*
 * Date          Author       Note
 * 2015.05.28    wzwz         进度条
 */




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "com.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "mhstatic.h"
#include "keypane.h"
#include "progress.h"
#include "update_net.h"


/*
 *
 */
static void CreateProgressInterface(HWND hWnd)
{
    HWND hTmp;
    CreateWindow(MH_LABEL_NAME,
        "0",
        WS_CHILD | WS_VISIBLE,
        IDC_TIP,
        222,
        60,
        40,
        30,
        hWnd, MH_LABEL_BKGND_3GREEN | MH_LABEL_TEXT_GREEN);
    hTmp = CreateWindow(MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ENTER,
        103,
        96,
        CX_DAT_ENTER_BMP,
        CY_DAT_ENTER_BMP,
        hWnd, (int)&g_bmp[BMP_DAT_ENTER]);
    ShowWindow(hTmp, SW_HIDE);

    hTmp = CreateWindow(MH_STATIC_NAME,
        GetString(786),
        WS_CHILD | WS_VISIBLE,
        ID_UPDATE,
        40,
        96,
        CX_DAT_ENTER_BMP,
        CY_DAT_ENTER_BMP,
        hWnd, (int)&g_bmp[BMP_RETRY]);
    ShowWindow(hTmp, SW_HIDE);

    hTmp = CreateWindow(MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_ASK_DLG_BMP - 40 - CX_NO_BMP,
        96,
        CX_DAT_ESC_BMP,
        CY_DAT_ESC_BMP,
        hWnd, (int)&g_bmp[BMP_DAT_ESC]);
    ShowWindow(hTmp, SW_HIDE);
}
/*
 *
 */
static void* Update(void *arg)
{
    HWND hWnd = (HWND)arg;
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pwnd_update)
    {
        switch (data.pwnd_update->type)
        {
        case UPDATE_TYPE_RACK:
            UpgradeJijia(&data.pwnd_update->error, &data.pwnd_update->progress);
            break;
        case UPDATE_TYPE_HEADER:
            UpdateHeader(g_knit_data, data.pwnd_update->file,
                    &data.pwnd_update->error, &data.pwnd_update->progress);
            break;
        case UPDATE_TYPE_BOARD:
            UpdateBoard(g_knit_data, data.pwnd_update->file,
                    &data.pwnd_update->error, &data.pwnd_update->progress);
            break;
        case UPDATE_TYPE_APP:
             UpgradeApp(&data.pwnd_update->error, &data.pwnd_update->progress);
            break;
        case UPDATE_TYPE_GAUZE:
            UpdateGauze(g_knit_data, data.pwnd_update->file,
                    &data.pwnd_update->error, &data.pwnd_update->progress);
            break;
        default:
            break;
        }
    }
    pthread_exit(0);
}
/*
 *
 */
 static void  error_tip(HWND hWnd, char error, char *out)
 {
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pwnd_update->type == UPDATE_TYPE_RACK)
    {
        switch(error)
        {
            case 255:
                {
                    strcat(out, GetString(924));
                }
                break;
            case 254:
                {

                }
                break;
        }
    }
    else if(data.pwnd_update->type == UPDATE_TYPE_APP)
    {
         switch(error)
        {
            case 255:
                {
                    strcat(out, GetString(924));
                }
                break;
        }
    }
    else if(data.pwnd_update->type == UPDATE_TYPE_BOARD)
    {

    }
    else if(data.pwnd_update->type == UPDATE_TYPE_HEADER)
    {

    }
    else if(data.pwnd_update->type == UPDATE_TYPE_GAUZE)
    {

    }
 }

static void OnTimer(HWND hWnd)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    enum {
        UPDATE_OK = 0,
        UPDATE_ERROR = 1,
        UPDATE_WAIT = 2,
    }update = UPDATE_WAIT;
    char error = 0;
    char progress = 0;
    if(data.pwnd_update)
    {
        if(data.pwnd_update->error)
        {
            update = UPDATE_ERROR;
            error = data.pwnd_update->error;
            data.pwnd_update->progress = -1;
        }
        else if(data.pwnd_update->progress == 100)
        {
            update = UPDATE_OK;
            data.pwnd_update->progress = -1;
        }
        else
        {
            update = UPDATE_WAIT;
            progress = data.pwnd_update->progress;
        }
    }
    else
    {
        update = UPDATE_ERROR;
        error = -128;
    }
    switch(update)
    {
    case UPDATE_OK:
        {
            KillTimer(hWnd, TIMER_ID_UPDATE);
            HWND hTmp = GetDlgItem(hWnd, ID_ENTER);
            ShowWindow(hTmp, SW_SHOWNORMAL);
            hTmp = GetDlgItem(hWnd, IDC_TIP);
            SetWindowCaption(hTmp, GetString(784));
            MoveWindow(hTmp, 20, 60, 240, 30, TRUE);
        }
        break;
    case UPDATE_ERROR:
        {
            KillTimer(hWnd, TIMER_ID_UPDATE);
            HWND hTmp = GetDlgItem(hWnd, ID_ESC);
            ShowWindow(hTmp, SW_SHOWNORMAL);

            hTmp = GetDlgItem(hWnd, ID_UPDATE);
            ShowWindow(hTmp, SW_SHOWNORMAL);

            hTmp = GetDlgItem(hWnd, IDC_TIP);
            char sc[64] = {0};
            strcat(sc, GetString(785));
            error_tip(hWnd,error, sc);
            SetWindowCaption(hTmp, sc);
            MoveWindow(hTmp, 20, 60, 240, 30, TRUE);
        }
        break;
    case UPDATE_WAIT:
        if(progress != data.pwnd_update->last_progress)
        {
            HWND hTmp = GetDlgItem(hWnd, IDC_TIP);
            char sc[32] = {0};
            sprintf(sc, "%d%%\n", progress);
            SetWindowCaption(hTmp, sc);
            InvalidateRect(hTmp, NULL, TRUE);
            RECT rect = {20, 60, 220, 90};
            InvalidateRect(hWnd, &rect, TRUE);
            data.pwnd_update->last_progress = progress;
        }
        break;
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    switch(nID)
    {
    case ID_ESC:
    case ID_ENTER:
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_UPDATE:
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_update)
            {
                data.pwnd_update->error = 0;
                data.pwnd_update->progress = 0;
            }
            pthread_t id;
            SetTimer(hWnd, TIMER_ID_UPDATE, 5);
            HWND hTmp = GetDlgItem(hWnd, ID_ENTER);
            ShowWindow(hTmp, SW_HIDE);
            hTmp = GetDlgItem(hWnd, ID_ESC);
            ShowWindow(hTmp, SW_HIDE);
            hTmp = GetDlgItem(hWnd, ID_UPDATE);
            ShowWindow(hTmp, SW_HIDE);
            hTmp = GetDlgItem(hWnd, IDC_TIP);
            SetWindowCaption(hTmp, "0");
            MoveWindow(hTmp, 222, 60, 40, 30, TRUE);
            pthread_create(&id, NULL, Update, (void*)hWnd);
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    switch(key)
    {
    case SCANCODE_ESCAPE:
        OnBtnCmd(hWnd, ID_ESC);
        break;
    case SCANCODE_ENTER:
        OnBtnCmd(hWnd, ID_ENTER);
        break;
    }
}
/*
 *
 */
static int ProgressWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateProgressInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.top += MASTER_TITLE_TOP;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;

            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));

            DrawText(hdc, GetWindowCaption(hWnd), -1, &rect, DT_CENTER
                    | DT_VCENTER | DT_SINGLELINE);

            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_update)
            {
                FREE(data.pwnd_update);
                SetWindowAdditionalData(hWnd, 0);
            }
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            HDC hdc = GetClientDC(hWnd);
            //RECT rect;
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_ASK_DLG]);

            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_update)
            {
                int progress = data.pwnd_update->progress;
                if(progress >= 0 && progress <= 100)
                {
                    int width = 198;
                    int mid = data.pwnd_update->progress * width / 100;
                    if(mid)
                    {
                        SetBrushColor(hdc, COLOR_HQ_YELLOW(hdc));
                        FillBox(hdc, 21, 60, mid, 30);
                    }
                    Rectangle(hdc, 20, 60, 219, 89);
                }
            }

            ReleaseDC(hdc);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_TIMER:
        {
            uint id = wParam;
            if(id == TIMER_ID_UPDATE)
            {
                OnTimer(hWnd);
            }
        }
        break;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateProgressWindow(HWND hHosting, const char *file, uchar type)
{
    int x = (CX_SCREEN - CX_ASK_DLG_BMP) / 2;
    int y = (CY_SCREEN - CY_ASK_DLG_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(250);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = ProgressWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_ASK_DLG_BMP;
    CreateInfo.by = y + CY_ASK_DLG_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    PWND_UPDATE p = calloc(1, sizeof(WND_UPDATE));
    if(p)
    {
        strcat(p->file, file);
        p->type = type;
    }
    CreateInfo.dwAddData = (int)p;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_ESC));
        ShowWindow(hWnd, SW_SHOWNORMAL);
        pthread_t id;
        SetTimer(hWnd, TIMER_ID_UPDATE, 5);
        pthread_create(&id, NULL, Update, (void*)hWnd);
    }
    return hWnd;
}
